// pages/project_list_detail/project_list_detail.js
const request = require('../../utils/promise.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userId: null,
    isDecisionUser: false, // 决策社员才能修改项目状态
    projectId: 0,
    projectObj: {
      projectName: null,
      constitution: null,
      projectDesc: null,
      projectStatus: 0,
      beginTime: null,
      endTime: null,
      created: null,
      username: null
    },
    projectIndex: 0,
    projectStatusDisabled: true, // 项目状态是否禁用(完成与关闭状态，属于禁用情况)
    projectStatusArray: [
      {value: 1, label: '未启动'},
      {value: 2, label: '运行'},
      {value: 3, label: '暂停'},
      {value: 4, label: '完成'},
      {value: 5, label: '关闭'}
    ],
    projectManagerFlag: false, // 是否项目负责人标识
    navTitle: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      projectId: options.id,
      userId: wx.getStorageSync('userId'),
      isDecisionUser: options.userVip=='100'? true: false // 是否决策社员
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initDetailDate();
  },

  /**
   * 页面被用户分享时执行
   */
  onShareAppMessage() {
    return {
      title: '项目详情'
    }
  },

  /**
   * 分享到朋友圈
   */
  onShareTimeline() {
    return {
      title: '项目详情'
    }
  },

  /**
   * 初始化详情页面数据
   */
  initDetailDate() {

    request.GET("/project/getProjectById", {projectId: this.data.projectId}).then(res => {
      // console.log(res)
      if (!this.data.navTitle) {
        wx.setNavigationBarTitle({
          title: res.data.projectName,
        })
      }

      this.setData({
        projectObj: res.data,
        navTitle: res.data.projectName,
      })

      // 项目picker的下标值
      let projectStatus = res.data.projectStatus;
      // 项目原来的状态 属于 完成与关闭，则禁止修改
      if (projectStatus==4 || projectStatus==5) {
        this.setData({
          projectStatusDisabled: true
        })
      } else {
        this.setData({
          projectStatusDisabled: false
        })
      }

      // 获得下标，初始化picker的默认值
      let projectIndex = (this.data.projectStatusArray || []).findIndex((item) => item.value == projectStatus);
      this.setData({
        projectIndex: projectIndex
      })
    });
    
    // 是否项目负责人
    request.GET("/project/getProjectManagerFlag", {projectId: this.data.projectId, userId: this.data.userId}).then(res => {
      let flag = res.data=='y'? true: false;
      this.setData({
        projectManagerFlag: flag
      })
    })
  },

  /**
   * 项目状态修改
   */
  bindPickerChange: function (e) {
    
    let projectIndex = e.detail.value;
    let oldValue = this.data.projectObj.projectStatus;
    let changeValue = this.data.projectStatusArray[projectIndex].value;

    // 项目状态，修改前后相同的情况
    if (oldValue==changeValue) {
      wx.showToast({
        title: '状态相同，无需修改',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    // 想法状态的项目，无法直接暂停，需要选择运行后再暂停
    if (oldValue==1 && changeValue==3) {
      wx.showModal({
        title: '温馨提示',
        content: '未启动状态的项目，无法直接暂停，需要选择运行后再暂停',
        showCancel: false
      })
      return;
    }
    // 想法状态的项目，不能选择完成，只能选择关闭
    if (oldValue==1 && changeValue==4) {
      wx.showModal({
        title: '温馨提示',
        content: '未启动状态的项目，无法直接完成，可以选择关闭',
        showCancel: false
      })
      return;
    }
    // 已运行或 暂停的项目状态，无法改回原来的，想法状态
    if ((oldValue==2 || oldValue==3) && changeValue==1) {
      wx.showModal({
        title: '温馨提示',
        content: '已运行或暂停的项目，无法改回 未启动状态',
        showCancel: false
      })
      return;
    }

    // picker下标修改
    this.setData({
      projectIndex: projectIndex
    })
    let param = {
      projectId: this.data.projectId,
      projectStatus: changeValue,
      userId: this.data.userId
    }
    let that = this;
    request.POST('/project/updateProjectStatus', param).then(res => {
      if (res.statusCode==200) {
        wx.showToast({
          title: '保存成功',
          icon: 'none',
          duration: 1000
        })
        that.initDetailDate();
      } else {
        wx.showToast({
          title: '保存异常失败',
          icon: 'error',
          duration: 1500
        })
      }
    })
  },

  /**
   * 跳转修改描述
   */
  gotoUpdateDesc(e) {
    let pageType = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/project_list_edit/project_list_edit?projectId='+ this.data.projectId
      +'&projectName='+ this.data.projectObj.projectName
      +'&pageType='+ pageType
      +'&projectStatusDisabled='+ this.data.projectStatusDisabled
      +'&projectManagerFlag='+ this.data.projectManagerFlag
    })
    wx.setStorageSync('projectDesc', this.data.projectObj.projectDesc);
  },

  /**
   * 跳转修改动态
   */
  gotoUpdateConstitution(e) {
    let pageType = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/project_list_edit/project_list_edit?projectId='+this.data.projectId
      +'&projectName='+this.data.projectObj.projectName
      +'&pageType='+pageType
      +'&projectStatusDisabled='+this.data.projectStatusDisabled
      +'&projectManagerFlag='+this.data.projectManagerFlag
    })
    wx.setStorageSync('constitution',this.data.projectObj.constitution);
  },

  /**
   * 跳转项目负责人列表
   */
  gotoProjectUserList() {
    wx.navigateTo({
      url: '/pages/project_user_list/project_user_list?projectId='+this.data.projectId
      +'&projectName='+this.data.projectObj.projectName
      +'&projectStatus='+this.data.projectObj.projectStatus
      +'&projectManagerFlag='+this.data.projectManagerFlag
    })
  },

  /**
   * 跳转任务列表
   */
  gotoProjectTaskList() {
    wx.navigateTo({
      url: '/pages/task/task?projectId='+ this.data.projectId +'&projectName='+ this.data.projectObj.projectName
    })
  },

  /**
   * 跳转项目--社员列表
   */
  gotoProjectMemberList() {
    // 项目负责人才能添加成员 
    wx.navigateTo({
      url: '/pages/project_member/project_member?projectId='+ this.data.projectId +'&isManager='+ (this.data.projectManagerFlag? 1: 0) +'&projectName='+ this.data.projectObj.projectName,
    })
  },

  /**
   * 点击删除
   */
  clickDelProject() {

    const param = {projectId: this.data.projectId, projectStatus: -1, userId: this.data.userId}
    wx.showModal({
      title: '温馨提示',
      content: '确定要删除该项目吗?',
      success (res) {
        if (res.confirm) {
          wx.showToast({
            title: '正在删除',
            icon: 'loading',
            duration: 30000,
            mask: true
          })

          request.POST('/project/updateProjectStatus', param).then(res => {
            // console.log(res)
            if (res.statusCode==200) {
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 1000,
                mask: true
              })

              setTimeout(function() {
                wx.navigateBack({
                  delta: 1,
                })
              }, 1000)
            } else {
              wx.showToast({
                title: '删除异常',
                icon: 'error',
                duration: 1500,
                mask: true
              })
            }
          })
        } 
      }
    })
  },

})